package com.hongtao3.hsync.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Arrays;
import java.util.Comparator;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;

import com.hongtao3.hsync.Main;
import com.hongtao3.hsync.ModifiedFlowLayout;

abstract public class ResourcesPanel extends JPanel {
    public ResourcesPanel(){
    	setLayout(new BorderLayout());
    	addTopPanel();
    	addCenterPanel();
    }
    private void addTopPanel(){
        JPanel p=new JPanel(new BorderLayout()); 
        
        JPanel leftPanel=new JPanel(new FlowLayout());
        typeLabel=new JLabel();
        leftPanel.add(typeLabel);
        MyButton homeButton=new MyButton(Main.getImageIcon("home.png", 20, 20));
        leftPanel.add(homeButton) ;
        MyButton upButton= new MyButton(Main.getImageIcon("up.png",20,20));
        leftPanel.add(upButton) ;
        upButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				goParen();
			}
		 });
        homeButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				goHome();
			}
		});
        
        MyButton refreshBt = new MyButton(Main.getImageIcon("refresh.png",20,20));
        leftPanel.add(refreshBt) ;
        refreshBt.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				refresh();
			}
		});
        leftPanel.add(new JLabel(Main.getImageIcon("folder_1.png"))) ;
        
        p.add(leftPanel,BorderLayout.WEST);
         
        JPanel rightPanel=new JPanel(new BorderLayout());
        pathBox=new JComboBox();
        pathBox.setEditable(true);
        rightPanel.add(pathBox,BorderLayout.CENTER);
        p.add(rightPanel,BorderLayout.CENTER);
        rightPanel.setBorder(new EmptyBorder(3, 2, 3, 2) );
     	
     	 add(p,BorderLayout.NORTH);
     	
     	pathBox.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String path=(String)pathBox.getSelectedItem();
				repaintPanel(path);
			}
		});
     }
    public void goHome(){
    	showPath(homePath);
    }

    private void addCenterPanel(){
    	centerPanel=new JPanel();
    	centerPanel.setLayout(new ModifiedFlowLayout(FlowLayout.LEFT));
    	JScrollPane sp=new JScrollPane(centerPanel);
    	add(sp,BorderLayout.CENTER);
    }
    public void showPath(String path){
    	pathBox.removeAllItems();
    	pathBox.addItem(path);
    }
    public void showPath(String path,boolean asHome){
    	showPath(path);
    	if(asHome){
    		this.homePath=path;
    	}
    }
    public void setType(String type){
    	typeLabel.setText(type);
    }
    
    public void setHomePath(String home){
    	this.homePath=home;
    }
    
    public File getFile(String path){
    	return new File(path);
    }
    
    private void goParen(){
    	String path=(String)pathBox.getSelectedItem();
    	if(path==null)return;
    	File f=getFile(path);
       if(f.getParent()!=null){
    	   showPath(f.getParent());
       }
    }
    private void refresh(){
    	repaintPanel((String)pathBox.getSelectedItem());
    }
    
    protected void repaintPanel(String path){
    	if(path==null)return;
    	File f=getFile(path);
    	if(!f.exists() || f.isHidden() || !f.isDirectory())return;
    	centerPanel.removeAll();
    	if(f.isFile()){
    		centerPanel.add(new FileButton(f,this));
    	}else if(f.isDirectory()){
    		File[] files=f.listFiles();
    		if(files==null){
    			centerPanel.repaint();
    			return;
    		}
    	    filesSort(files);
    		for(File _f:files){
    			if(_f.isHidden())continue;
    			centerPanel.add(new FileButton(_f,this));
    		}
    	}
    	centerPanel.repaint();
    	centerPanel.validate();
    }
    
    protected void filesSort(File[] files){
    	Arrays.sort(files, new Comparator() {
    		public int compare(final Object o1, final Object o2) {
    			return ((File)o2).getName().toLowerCase().compareTo(((File)o1).getName().toLowerCase());
    		}
    	}); 
    	Arrays.sort( files, new Comparator() {
    		public int compare(final Object o1, final Object o2) {
    			return ((File)o2).isDirectory()?1:0;
    		}
    	}); 
    }
    
    private JComboBox pathBox;
    private JPanel centerPanel;
    private JLabel typeLabel;
    private String homePath;
}
